Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald\include\herald\engine\activities.h
Line
Count
Source
1
//  Copyright 2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#ifndef HERALD_ACTIVITIES_H
6
#define HERALD_ACTIVITIES_H
7
8
#include "../context.h"
9
#include "../datatype/data.h"
10
#include "../datatype/target_identifier.h"
11
12
#include <memory>
13
#include <functional>
14
#include <optional>
15
#include <tuple>
16
17
namespace herald {
18
namespace engine {
19
20
using namespace herald::datatype;
21
22
// Types used in coordination of Herald sensor activities
23
24
/// \brief Herald implementation provided Feature tag/identifier
25
using FeatureTag = herald::datatype::Data;
26
27
/// \brief Lists all Features currently supported by Herald providers
28
namespace Features {
29
  /// \brief Herald Bluetooth protocol connection is the first supported dependency type.
30
  static FeatureTag HeraldBluetoothProtocolConnection = herald::datatype::Data(std::byte(0x01),1);
31
}
32
33
/// \brief A relative priority between different activities
34
using Priority = std::uint8_t;
35
36
/// \brief Convenience Priority values
37
///
38
/// Try to not use the same value, but rather offset by 5 or 10 between different dependencies.
39
/// Be sure to avoid circular priority dependencies.
40
namespace Priorities {
41
  constexpr Priority Critical(200);
42
  constexpr Priority High(150);
43
  constexpr Priority Default(100);
44
  constexpr Priority Low(50);
45
}
46
47
struct Activity; // fwd decl
48
49
/// \brief An absolute prerequisite required before an activity can take place
50
///
51
/// An example would be the presence of a Bluetooth connection to a specified Device.
52
using Prerequisite = std::tuple<FeatureTag,std::optional<TargetIdentifier>>;
53
/// \brief a Presrequisite with a relative priority assigned to assist Herald to prioritise effectively.
54
using PrioritisedPrerequisite = std::tuple<FeatureTag,Priority,std::optional<TargetIdentifier>>;
55
56
57
// THE FOLLOWING IS FOR PLATFORMS WITH CALLBACK / STD::ASYNC+STD::FUTURE SUPPORT
58
/** callback for open and close connection **/
59
//using ConnectionCallback = std::function<void(const std::vector<PrioritisedPrerequisite> connectedTo)>;
60
61
/** Callback function or lambda provided by the Coordinator to be called once the action completes **/
62
//using CompletionCallback = std::function<void(const Activity,std::optional<Activity>)>; // function by value
63
64
/** Activity execution function or lambda **/
65
//using ActivityFunction = std::function<void(const Activity,CompletionCallback)>; // function by value
66
67
// THE FOLLOWING IS FOR PLATFORMS WITHOUT STD::ASYNC SUPPORT (i.e. that are SYNC ONLY)
68
69
/// \brief A convenience Function alias that invokes an activity and optionally returns a follow-on activity.
70
using ActivityFunction = std::function<std::optional<Activity>(const Activity)>;
71
72
// END RESULTS ONLY PLATFORMS
73
74
/// \brief An activity that needs to be performed due to some state being achieved in a Sensor
75
struct Activity {
76
  /// \brief The relative priority for this Activity compared to the scale
77
  /// \sa Priorities
78
  Priority priority;
79
  /// \brief A human readable name for this activity used for logging.
80
  std::string name;
81
  /// \brief A list of non-prioritised pre-requisities (priority is taken from the priority field in Activity).
82
  std::vector<Prerequisite> prerequisites; // no target id means all that are connected
83
  /// \brief The Activity function to call when all prerequisites have been met. May not be called.
84
  ActivityFunction executor;
85
};
86
87
/// \brief Coordination management class that arranges Sensor's periodic requirements and activity interdependencies.
88
///
89
/// Some sensors may have dependencies on others, or system features.
90
/// This class provides a way of Sensors to let the Herald system know
91
/// of their requirements and capabilities at any given moment.
92
///
93
class CoordinationProvider {
94
public:
95
8
  CoordinationProvider() = default;
96
8
  virtual ~CoordinationProvider() = default;
97
  
98
  // Coordination methods - Since v1.2-beta3
99
  /// What connections does this Sensor type provide for Coordination
100
  virtual std::vector<FeatureTag> connectionsProvided() = 0;
101
102
  /// \brief Runtime connection provisioning (if it isn't requested, it can be closed)
103
  ///
104
  /// Note:  WITH STD::SYNC ONLY: virtual void provision(const std::vector<PrioritisedPrerequisite>& requested, const ConnectionCallback& connCallback) = 0;
105
  virtual std::vector<PrioritisedPrerequisite> provision(const std::vector<PrioritisedPrerequisite>& requested) = 0;
106
107
  // Runtime coordination callbacks
108
  /// \brief Get a list of what connections are required to which devices now (may start, maintain, end (if not included))
109
  virtual std::vector<PrioritisedPrerequisite> requiredConnections() = 0;
110
  /// \brief Get a list of activities that are currently outstanding in this iteration
111
  virtual std::vector<Activity> requiredActivities() = 0;
112
};
113
114
}
115
}
116
117
#endif